<?php
namespace Tjise;

class TjiseAPP{
    private $Token_edudot_header = null;
    private $Token_tjise_header = null;
    private $TjiseApi = [
        'GetLoginToken' => 'http://api.edudot.cn/usercenter/connect/token',  /**获取登陆凭证 POST*/  
        'Home'          => 'http://api.edudot.cn/edudotapi/api/v1/Apps',    /**首页 GET*/ 

        'LateSignIn'    => 'http://api.tjise.edudot.cn/api/v1/LateSignIn/SignIn',   /**考勤签到 POST*/ 
        'MorningRunning'=> 'http://api.tjise.edudot.cn/api/v1/Motions/ClockIn',  /**晨跑打卡 POST*/
        'Health'        => 'http://api.tjise.edudot.cn/api/v1/Health/BodyTemperature'    /**健康打卡 POST*/   
    ];
    private $fun = [
        'Home'=>1,
        'LateSignIn'=>1,
        'MorningRunning_on'=>1,
        'MorningRunning_off'=>1,
        'Health'=>1
    ];

    
    public function request($fun){
        // return $this->$fun();
        return array_key_exists($fun, $this->fun) ? $this->$fun() : 'No function' ;
    }

    public function login($user,$passwd){
        $login_header = [
            'Connection: keep-alive',
            'Cache-Control: max-age=0',
            'Origin: http://api.edudot.cn',
            'Upgrade-Insecure-Requests: 1',
            'Content-Type: application/x-www-form-urlencoded',
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
            'Referer: http://api.edudot.cn/usercenter/connect/token',
            'Accept-Encoding: gzip, deflate',
            'Accept-Language: zh-CN,zh;q=0.9'
        ];
        $config = [
            'method'=>'POST',
            'url'=>$this->TjiseApi['GetLoginToken'],
            'body'=>"password=".$passwd."&grant_type=password&login_type=&client_id=DDE1F5ACAF194674B13167269861FB7D&username=".$user
        ];

        $token = json_decode($this->Curl($config,$login_header),true)['access_token'];

        $login_header[] =  'Authorization: Bearer '.$token;
        $this->Token_edudot_header = $login_header;

        $this->Token_tjise_header = [
            'Host: api.tjise.edudot.cn',
            'Origin: http://clock.tjise.edudot.cn',
            'X-Requested-With: XMLHttpRequest',
            'User-Agent: Mozilla/5.0 (Linux; Android 10; Mi 10 Build/QKQ1.191117.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045514 Mobile Safari/537.36; Edudot',
            'credentials: include',
            'Content-Type: application/json;charset=UTF-8',
            'Accept: */*',
            'Referer: http://clock.tjise.edudot.cn/',
            'Accept-Encoding: gzip, deflate',
            'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
            'Connection: close',
            'Authorization: Bearer '.$token
        ];

        return $this->Token_tjise_header;
    }

    public function Home(){

        $config = [
            'method'=>'GET',
            'url'=>$this->TjiseApi['Home']
        ];

        return json_decode($this->Curl($config,$this->Token_edudot_header),true);
    }

    public function LateSignIn(){
        $config = [
            'method'=>'POST',
            'url'=>$this->TjiseApi['LateSignIn'],
            'body'=>json_encode([
                "signInLocation"=>"天津市西青区泮缘道18号靠近天津市大学软件学院学术交流中心",
                "remark"=>"",
                "longitude"=>117.106873,
                "latitude"=>39.06697
            ])
        ];

        return json_decode($this->Curl($config,$this->Token_tjise_header),true);
    } 

    public function Health(){

        //时间戳
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        
        $config = [
            'method'=>'POST',
            'url'=>$this->TjiseApi['Health'],
            'body'=>json_encode([
                "images"=>"532254CD-2903-4BF5-946C-DD2C30119A8F",
                "measureDate"=>$msectime,
                "bodyTemperature"=>"36.5",
                "measureTimeType"=>1,
                "remark"=>"身体健康良好，无发热感冒症状",
                "latitude"=>39.070773,
                "longitude"=>117.108545,
                "location"=>"天津市西青区卓越北路16号靠近天津市大学软件学院",
                "isContactPatient"=>0
            ])
        ];

        return json_decode($this->Curl($config,$this->Token_tjise_header),true);
    } 

    public function MorningRunning_on(){
        $config = [
            'method'=>'POST',
            'url'=>$this->TjiseApi['MorningRunning'],
            'body'=>json_encode([
                "status"=>0,
                "longitude"=>117.106858,
                "latitude"=>39.067142,
                "address"=>"天津市西青区泮缘道18号靠近天津工业大学新校区"
            ])
        ];

        return json_decode($this->Curl($config,$this->Token_tjise_header),true);
        
    } 


    public function MorningRunning_off(){
        $config = [
            'method'=>'POST',
            'url'=>$this->TjiseApi['MorningRunning'],
            'body'=>json_encode([
                "status"=>1,
                "longitude"=>117.111607,
                "latitude"=>39.063954,
                "address"=>"天津市西青区学缘道399号靠近天津工业大学图书馆"
            ])
        ];

        return json_decode($this->Curl($config,$this->Token_tjise_header),true);
    } 

/**
 *  网络请求
 *   参数说明
 *      $header  ====>  请求头
 *      $config     ====>   url   |  请求方式  |   post参数
 *          $config[
 *              'url'=>''
 *              'method'=>'GET | POST'
 *               'body'=>''        
 *          ]     
 * 
 */
    private function Curl($config,$header){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_URL, $config['url']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
        if($config['method'] == 'POST'){
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $config['body']);
        }
        curl_setopt($ch, CURLOPT_TIMEOUT,5);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
        $data = curl_exec($ch);
        $res = curl_getinfo($ch, CURLINFO_HEADER_OUT);
        $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
        // $data['code'] = $httpCode;
        curl_close($ch);
        return $data;
    }

}

?>